package com.yanjue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanjue.common.entity.IndexBanner;
import com.yanjue.common.entity.IndexBannerItem;
import com.yanjue.common.redisKey.RedisExpire;
import com.yanjue.common.redisKey.RedisHomepageKey;
import com.yanjue.common.redisKey.RedisService;
import com.yanjue.common.util.CommonUtil;
import com.yanjue.mapper.IndexBannerItemMapper;
import com.yanjue.mapper.IndexBannerMapper;
import com.yanjue.service.IndexBannerService;
import com.yanjue.vo.BannerItemVO;
import com.yanjue.vo.IndexBannerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author yanju
* @description 针对表【index_banner(精品页Banner配置)】的数据库操作Service实现
* @createDate 2025-01-16 12:33:44
*/
@Service
public class IndexBannerServiceImpl extends ServiceImpl<IndexBannerMapper, IndexBanner>
    implements IndexBannerService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private IndexBannerMapper indexBannerMapper;
    @Autowired
    private IndexBannerItemMapper indexBannerItemMapper;

    /**
     * 获取 bannervo
     * @param bannerId
     * @return
     */
    @Override
    public IndexBannerVO getBannerVO(Integer bannerId) {
        //1.获取key 查redis
        String key = RedisHomepageKey.getBannerVoKey(bannerId);
        IndexBannerVO vo = this.redisService.getCache(key, IndexBannerVO.class);
        if (vo != null){
            return vo;
        }
        //2.缓存中没有，查数据库
        IndexBanner indexBanner = this.indexBannerMapper.selectById(bannerId);
        if (indexBanner == null){
            //2.1.数据库也没有·
            return null;
        }

        //3.有banner 查 bannerItem 明细
        LambdaQueryWrapper<IndexBannerItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndexBannerItem::getBannerId,bannerId)
                .orderByAsc(IndexBannerItem::getSortNumber);
        List<IndexBannerItem> indexBannerItems = this.indexBannerItemMapper.selectList(queryWrapper);
        if (CommonUtil.isEmpty(indexBannerItems)){
            //3.1.bannerItem 为空
            return null;
        }

        //4.此时都不为空 搞vo
        List<BannerItemVO> itemsVO = new ArrayList<>();
        for (IndexBannerItem indexBannerItem : indexBannerItems) {
            BannerItemVO item = new BannerItemVO();
            BeanUtils.copyProperties(indexBannerItem,item);
            itemsVO.add(item);
        }
        //5.给indexBannerVO设置值
        vo = new IndexBannerVO(indexBanner.getId(),indexBanner.getName(),itemsVO);
        //6.存入redis
        this.redisService.setExpireCache(key,vo, RedisExpire.HOUR_FOUR);

        return vo;
    }
}




